package com.kvn.mockj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Helper {
    private final static char A = 'A', Z = 'Z';

    public static String capitalize(String str) {
        char [] chars = str.toCharArray();
        if(chars[0] >= A && chars[0] <= Z){
            chars[0] += 32;
        }
        return String.valueOf(chars);
    }
    public static String upper(String str){return str.toUpperCase();}
    public static String lower(String str){return str.toLowerCase();}

    public static <T> T pick(T ...item) {
        return pick(item, 1, 1);
    }
    public static <T> T pick(T[] array, int min, int max) {
        min = min == 0 ? 1 : min;
        max = max == 0 ? 1 : max;
        if (min == 1 && max == 1) {
            return array[Basic.natural(array.length)];
        } else if(max > 1) {
            return shuffle(array, min, max);
        }else{
            return shuffle(array, min, max);
        }
    }
    public static <T> T shuffle(T[] array, int max) {
        return shuffle(array, 0, max);
    }
    public static <T> T shuffle(T[] array, int min, int max){
        T[] arr = Arrays.copyOfRange(array, 0, array.length);
        ArrayList<T> old = new ArrayList<T>(Arrays.asList(arr));
        int index = 0, length = array.length;
        List<T> result = new ArrayList<T>();
        for(int i = 0; i <= length; i++){
            index = Basic.natural(0, arr.length -1);
            result.add(arr[index]);
            old.remove(index);
        }
        if(min == 0 && max == 0) {
            return result.get(0);
        } else if (max >= 0) {
            index = Basic.natural(min, max);
            return result.get(index);
        } else {
            index = Basic.natural(min, max);
            return result.get(index);
        }
    }

    public static <T> T[] concatAll(T [] first, T[] ...rest) {
        int totalLen = first.length;
        for (T[] array : rest) {
            totalLen += array.length;
        }
        T[] result = Arrays.copyOf(first, totalLen);
        int offset = first.length;
        for (T[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }
}
